module.exports = function() { 
    // 1. 读取已有的代理配置文件
    var proxyConfig = require('#/data/proxy-config.js')
    // 2. 读取html文件并进行代理
    var filesJson = JavaApi.files('@/src/html');
    var files = JSON.parse(filesJson);
    var frontDir = System.frontDir;
    
    // 3. 当前服务地址
    var target = 'http://127.0.0.1:' + System.port;
    
    // 动态构建代理
    var buildProxy = function(file, pageProxy) {
        // var resourceUrl = file.replace(frontDir, '@/');
        var resourceUrl = file; // 不替换更好吧
        // console.log(frontDir);
        // console.log("resourceUrl " + resourceUrl);
        // requestFileUrl src/html/second.html
        // requestUrl /html/second.html
        var requestUrl = JavaApi.resourceToUrl(resourceUrl);
        var proxyUrl = requestUrl.substring(requestUrl.lastIndexOf('/'), requestUrl.length);
        console.log("proxyUrl " + proxyUrl);
        console.log("target " + target);
        var pathRewrite = {};
        pathRewrite['^' + proxyUrl] = requestUrl;
        
        var proxy = {
            target: target,
            changeOrigin: true,
            pathRewrite: pathRewrite
        };
        pageProxy[proxyUrl] = proxy;
    };

    var pageProxy = {};
    for(var i in files) {
        var file = files[i];

        if(file.indexOf('.html') + 5 == file.length()) {
            // .html 结尾
            // 获取相对路径
            buildProxy(file, pageProxy);
        }
    }

    for(var key in pageProxy) {
        proxyConfig[key] = pageProxy[key];
    }

    return proxyConfig;
}();
